//
//  HMWelcomeViewController.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/12.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMWelcomeViewController.h"
#import "HMMainTabBarController.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "HMUserAccountViewModel.h"
#import "HMUserAccount.h"

@interface HMWelcomeViewController ()

@property(nonatomic,strong)UIImageView *bgImageView;
@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *welcomeLabel;

@end

@implementation HMWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self setUpSubViews];
    
    [self setImage];

    
//    每次来刷新一下,看看用户信息有没有该不变
    [[HMUserAccountViewModel sharedInstance] loadUserInfo:^(NSError *error) {
        if(error != nil)
        {
            HMLog(@"加载用户信息出错:%@",error);
            return ;
        }
//        重新
        
        [self setImage];
    }];
}

//-(instancetype)init
//{
//    if(self = [super init])
//    {
//        [self setUpSubViews];
//    }
//    return self;
//}

-(void)setUpSubViews
{
//    self.bgImageView.translatesAutoresizingMaskIntoConstraints = NO;
//    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
//    self.welcomeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
//    1.添加子控件
    self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    [self.view addSubview:self.bgImageView];
    self.iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
    [self.view addSubview:self.iconView];
    self.welcomeLabel = [[UILabel alloc] init];
    [self.view addSubview:self.welcomeLabel];
    
//    设置
    self.iconView.layer.cornerRadius = 42.5;
    self.iconView.layer.masksToBounds = YES;
    
    self.welcomeLabel.text = @"欢迎回来";
    self.welcomeLabel.font = HMWelcomeLabelFont;
    self.welcomeLabel.textColor = [UIColor blackColor];
    [self.welcomeLabel sizeToFit];

//    2.添加约束
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-160);
    }];
//
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView);
        make.top.equalTo(self.iconView.mas_bottom).offset(16);
    }];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self startAnimation];
}


-(void)startAnimation
{
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-([UIScreen mainScreen].bounds.size.height-160));
    }];
    
    [UIView animateWithDuration:1 delay:0.25 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
 
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
//        动画完成跳转界面,切换到主界面
        [getApplicationDelegate switchRootViewController:[HMMainTabBarController mainTabBarController]];
    }];
}

-(void)setImage
{
    //    加载用户头像,先查看缓冲池
    NSURL *url = [NSURL URLWithString:[HMUserAccountViewModel sharedInstance].userAccount.avatar_large];
    [self.iconView sd_setImageWithURL:url  placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
}

@end







